package com.roncoo.education.course.service.dao.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.roncoo.education.course.service.dao.CourseDao;
import com.roncoo.education.course.service.dao.impl.mapper.CourseMapper;
import com.roncoo.education.course.service.dao.impl.mapper.entity.Course;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseExample;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseExample.Criteria;

@Repository
public class CourseDaoImpl implements CourseDao {
    @Autowired
    private CourseMapper courseMapper;

    public int save(Course record) {
        return this.courseMapper.insert(record);
    }

    public int deleteById(Long id) {
        return this.courseMapper.deleteById(id);
    }

    public int updateById(Course record) {
    	record.setGmtCreate(null);
		record.setGmtModified(null);
        return this.courseMapper.updateById(record);
    }

    public Course getById(Long id) {
        return this.courseMapper.selectById(id);
    }

    public Page<Course> listForPage(int pageCurrent, int pageSize, QueryWrapper wrapper) {
//        int count = this.courseMapper.selectCount(example);
//        pageSize = PageUtil.checkPageSize(pageSize);
//        pageCurrent = PageUtil.checkPageCurrent(count, pageSize, pageCurrent);
//        int totalPage = PageUtil.countTotalPage(count, pageSize);
//        example.setLimitStart(PageUtil.countOffset(pageCurrent, pageSize));
//        example.setPageSize(pageSize);
//        return new Page<Course>(count, totalPage, pageCurrent, pageSize, this.courseMapper.selectByExample(example));
		Page<Course> coursePage = new Page<>(pageCurrent,pageSize);
		Page page = courseMapper.selectPage(coursePage, wrapper);
		return page;
	}

	@Override
	public List<Course> listByCategoryId(Long categoryId) {
//		CourseExample example = new CourseExample();
//		Criteria c = example.createCriteria();
//		c.andIdEqualTo(courseId);
//		List<Course> list = this.courseMapper.selectList(example);
//		return list;
		QueryWrapper<Course> eq = new QueryWrapper<Course>().eq("category_id1", categoryId)
				.or().eq("category_id2", categoryId)
				.or().eq("category_id3", categoryId);
		List<Course> courses = this.courseMapper.selectList(eq);
		return courses;
	}

	@Override
	public Course getByCourseIdAndStatusId(Long courseId, Integer statusId) {
//		CourseExample example = new CourseExample();
//		Criteria c = example.createCriteria();
//		c.andIdEqualTo(courseId);
//		c.andStatusIdEqualTo(StatusId);
//		List<Course> list = this.courseMapper.selectByExample(example);
//		if (list.isEmpty()) {
//			return null;
//		}
//		return list.get(0);
		QueryWrapper<Course> eq = new QueryWrapper<Course>().eq("id", courseId).eq("status_id", statusId);
		List<Course> courses = this.courseMapper.selectList(eq);
		if(courses.isEmpty()){
			return null;
		}
		return courses.get(0);
	}

	@Override
	public Course getByCourseName(String courseName) {
//		CourseExample example = new CourseExample();
//		Criteria c = example.createCriteria();
//		c.andCourseNameLike(PageUtil.rightLike(courseName));
//		List<Course> courseList =  this.courseMapper.selectByExample(example);
//		if (courseList.isEmpty()) {
//			return null;
//		}
//		return courseList.get(0);
		QueryWrapper<Course> course_name = new QueryWrapper<Course>().likeRight("course_name", courseName);
		List<Course> courses = this.courseMapper.selectList(course_name);
		if(courses.isEmpty()){
			return null;
		}
		return courses.get(0);
	}

	@Override
	public List<Course> listByCategoryId2AndStatusId(Long categoryId2, Integer statusId) {
//		CourseExample example = new CourseExample();
//		Criteria c = example.createCriteria();
//		c.andCategoryId2EqualTo(categoryId2);
//		c.andStatusIdEqualTo(statusId);
//		example.setOrderByClause("sort desc,id desc");
//		return this.courseMapper.selectByExample(example);
		QueryWrapper<Course> eq = new QueryWrapper<Course>().eq("category_id2", categoryId2).eq("status_id", statusId);
		eq.orderByDesc("sort,id");
		List<Course> courses = this.courseMapper.selectList(eq);
		return courses;
	}
}